//
//  ReportProjectDeviceInfoCell.m
//  efairy
//
//  Created by 李锋 on 2019/3/21.
//  Copyright © 2019年 East. All rights reserved.
//

#import "ReportProjectDeviceInfoCell.h"

@implementation ReportProjectDeviceInfoCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initView];
    }
    return self;
}



-(void)initView{
    
    _deviceName = [[UILabel alloc]init];//设备名称
    _deviceName.textColor = ColorFromRGB(0x333333);
    _deviceName.font = [UIFont boldSystemFontOfSize:16.f];
    
    _line = [[UIView alloc]init];
    _line.backgroundColor = ColorFromRGB(0xe5e5e5);
    
    
    _deviceTypeTitle = [[UILabel alloc]init];   // 设备类型
    _deviceTypeTitle.text = @"设备类型";
    _deviceTypeTitle.textColor = ColorFromRGB(0x333333);
    _deviceTypeTitle.font = [UIFont systemFontOfSize:14.f];
    
    _deviceType = [[UILabel alloc]init];
    _deviceType.textColor = ColorFromRGB(0x666666);
    _deviceType.font = [UIFont systemFontOfSize:14.f];
    _deviceType.textAlignment = NSTextAlignmentRight;
    _deviceType.numberOfLines = 0;
    
    
    _alarmTitle = [[UILabel alloc]init];
    _alarmTitle.text = @"报警次数";
    _alarmTitle.textColor = ColorFromRGB(0x333333);
    _alarmTitle.font = [UIFont systemFontOfSize:14.f];
    
    _alarm = [[UILabel alloc]init];
    _alarm.textColor = ColorFromRGB(0xFF0000);
    _alarm.font = [UIFont systemFontOfSize:14.f];
    _alarm.textAlignment = NSTextAlignmentRight;
    
    _warningTitle = [[UILabel alloc]init];
    _warningTitle.text = @"预警次数";
    _warningTitle.textColor = ColorFromRGB(0x333333);
    _warningTitle.font = [UIFont systemFontOfSize:14.f];
    
    _warning = [[UILabel alloc]init];
    _warning.textColor = ColorFromRGB(0xFF7200);
    _warning.font = [UIFont systemFontOfSize:14.f];
    _warning.textAlignment = NSTextAlignmentRight;
    
    _troubleTitle = [[UILabel alloc]init];
    _troubleTitle.text = @"故障次数";
    _troubleTitle.textColor = ColorFromRGB(0x333333);
    _troubleTitle.font = [UIFont systemFontOfSize:14.f];
    
    _trouble= [[UILabel alloc]init];
    _trouble.textColor = ColorFromRGB(0xFFB900);
    _trouble.font = [UIFont systemFontOfSize:14.f];
    _trouble.textAlignment = NSTextAlignmentRight;
    
    _deviceAddressTitle = [[UILabel alloc]init];//设备地址
    _deviceAddressTitle.text = @"设备地址";
    _deviceAddressTitle.textColor = ColorFromRGB(0x333333);
    _deviceAddressTitle.font = [UIFont systemFontOfSize:14.f];
    
    _deviceAddress = [[UILabel alloc]init];// 报表生成时间
    _deviceAddress.textColor = ColorFromRGB(0x666666);
    _deviceAddress.font = [UIFont systemFontOfSize:14.f];
    _deviceAddress.textAlignment = NSTextAlignmentRight;
    _deviceAddress.numberOfLines = 0;
    
    _deviceUserTitle = [[UILabel alloc]init];
    _deviceUserTitle.text = @"设备拥有者";
    _deviceUserTitle.textColor = ColorFromRGB(0x333333);
    _deviceUserTitle.font = [UIFont systemFontOfSize:14.f];
    _deviceUserTitle.layer.borderColor = [ColorFromRGB(0xB3B8BE)CGColor];
    _deviceUserTitle.layer.borderWidth = 1.0f;
    _deviceUserTitle.textAlignment = NSTextAlignmentCenter;
    
    
    _userPhone = [[UILabel alloc]init];;
    _userPhone.text = @"拥有人电话";
    _userPhone.textColor = ColorFromRGB(0x333333);
    _userPhone.font = [UIFont systemFontOfSize:14.f];
    _userPhone.layer.borderColor = [ColorFromRGB(0xB3B8BE)CGColor];
    _userPhone.layer.borderWidth = 1.0f;
    _userPhone.textAlignment = NSTextAlignmentCenter;
    
    _alarmTypeUIView = [[UIView alloc]init];
    _alarmTypeUIView.layer.borderColor = [ColorFromRGB(0xB3B8BE)CGColor];
    _alarmTypeUIView.layer.borderWidth = 1.0f;
    UITapGestureRecognizer *alarmTypeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickAlarmTypeView:)];
    [_alarmTypeUIView addGestureRecognizer:alarmTypeTap];
    
    _typeImage = [[UIImageView alloc]init];
    _typeImage.image = [UIImage imageNamed:@"icon_arrow_bottom"];
    
    _alarmType = [[UILabel alloc]init];//报警类型
    _alarmType.text = @"报警类型";
    _alarmType.textColor = ColorFromRGB(0x333333);
    _alarmType.font = [UIFont systemFontOfSize:12.f];
//    _alarmType.layer.borderColor = [ColorFromRGB(0xB3B8BE)CGColor];
//    _alarmType.layer.borderWidth = 1.0f;
    _alarmType.textAlignment = NSTextAlignmentCenter;
    _alarmType.numberOfLines = 2;
    
    [_alarmTypeUIView sd_addSubviews:@[_alarmType,_typeImage]];
    _alarmType.sd_layout.leftSpaceToView(_alarmTypeUIView, 0).rightSpaceToView(_alarmTypeUIView, 0).heightIs(30)
    .topSpaceToView(_alarmTypeUIView, 4);
    _typeImage.sd_layout.widthIs(12).heightIs(8).topSpaceToView(_alarmType, 2).leftSpaceToView(_alarmTypeUIView, (44-12)/2);
    
    
    
    
    _alarmDates = [[UILabel alloc]init];  // 报警时间段
    _alarmDates.text = @"报警时间段";
    _alarmDates.textColor = ColorFromRGB(0x333333);
    _alarmDates.font = [UIFont systemFontOfSize:12.f];
    _alarmDates.layer.borderColor = [ColorFromRGB(0xB3B8BE)CGColor];
    _alarmDates.layer.borderWidth = 1.0f;
    _alarmDates.textAlignment = NSTextAlignmentCenter;
    _alarmDates.numberOfLines = 2;
    
    _score = [[UILabel alloc]init];//峰值分数；
    _score.text = @"峰值分数";
    _score.textColor = ColorFromRGB(0x333333);
    _score.font = [UIFont systemFontOfSize:12.f];
    _score.layer.borderColor = [ColorFromRGB(0xB3B8BE)CGColor];
    _score.layer.borderWidth = 1.0f;
    _score.textAlignment = NSTextAlignmentCenter;
    _score.numberOfLines = 2;
    
    
    
    _riskUIView = [[UIView alloc]init];
    _riskUIView.layer.borderColor = [ColorFromRGB(0xB3B8BE)CGColor];
    _riskUIView.layer.borderWidth = 1.0f;
    UITapGestureRecognizer *riskTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickRiskView:)];
    [_riskUIView addGestureRecognizer:riskTap];
    
    _riskImage = [[UIImageView alloc]init];
    _riskImage.image = [UIImage imageNamed:@"icon_arrow_bottom"];
    
    _risk = [[UILabel alloc]init];//风险评估
    _risk.text = @"风险评估";
    _risk.textColor = ColorFromRGB(0x333333);
    _risk.font = [UIFont systemFontOfSize:12.f];
    _risk.textAlignment = NSTextAlignmentCenter;
    _risk.numberOfLines = 2;
    
    [_riskUIView sd_addSubviews:@[_risk,_riskImage]];
    _risk.sd_layout.leftSpaceToView(_riskUIView, 0).rightSpaceToView(_riskUIView, 0).heightIs(30)
    .topSpaceToView(_riskUIView, 4);
    _riskImage.sd_layout.widthIs(12).heightIs(8).topSpaceToView(_risk, 2).leftSpaceToView(_riskUIView, (44-12)/2);
    
    _line2 = [[UIView alloc]init];
    _line2.backgroundColor = ColorFromRGB(0xeff0f4);
    
    
    
    [self.contentView sd_addSubviews:@[_deviceName,_line,_deviceTypeTitle,_deviceType,_alarmTitle,_alarm,_warningTitle,_warning,
                                       _troubleTitle,_trouble,_deviceAddressTitle,_deviceAddress,_deviceUserTitle,_userPhone]];
    
    _deviceName.sd_layout.widthIs(SCREEN_WIDTH).heightIs(40).leftSpaceToView(self.contentView, 12).topSpaceToView(self.contentView, 0);
    _line.sd_layout.widthIs(SCREEN_WIDTH).heightIs(1).topSpaceToView(_deviceName, 0);
    _deviceTypeTitle.sd_layout.widthIs(68).heightIs(14).leftSpaceToView(self.contentView, 12).topSpaceToView(_line, 12);
    _deviceType.sd_layout.widthIs(SCREEN_WIDTH-68-24).topEqualToView(_deviceTypeTitle)
    .rightSpaceToView(self.contentView, 12).autoHeightRatio(0);
    
    _alarmTitle.sd_layout.widthIs(68).heightIs(14).leftSpaceToView(self.contentView, 12).topSpaceToView(_deviceType, 12);
    _alarm.sd_layout.widthIs(SCREEN_WIDTH-68-12).topEqualToView(_alarmTitle).leftSpaceToView(_alarmTitle, 0)
    .rightSpaceToView(self.contentView, 12).heightIs(14);
    
    _warningTitle.sd_layout.widthIs(68).heightIs(14).leftSpaceToView(self.contentView, 12).topSpaceToView(_alarm, 12);
    _warning.sd_layout.widthIs(SCREEN_WIDTH-68-24).topEqualToView(_warningTitle)
    .rightSpaceToView(self.contentView, 12).heightIs(14);
    
    _troubleTitle.sd_layout.widthIs(68).heightIs(14).leftSpaceToView(self.contentView, 12).topSpaceToView(_warning, 12);
    _trouble.sd_layout.widthIs(SCREEN_WIDTH-68-24).topEqualToView(_troubleTitle)
    .rightSpaceToView(self.contentView, 12).heightIs(14);
    
    _deviceAddressTitle.sd_layout.widthIs(68).heightIs(14).leftSpaceToView(self.contentView, 12).topSpaceToView(_trouble, 12);
    _deviceAddress.sd_layout.widthIs(SCREEN_WIDTH-68-24).topEqualToView(_deviceAddressTitle)
    .rightSpaceToView(self.contentView, 12).autoHeightRatio(0).minHeightIs(12);
    
    
    _deviceUserTitle.sd_layout.widthIs((SCREEN_WIDTH-24)/2).heightIs(40).leftSpaceToView(self.contentView, 12)
    .topSpaceToView(_deviceAddress, 12);
    
    _userPhone.sd_layout.widthIs((SCREEN_WIDTH-24)/2+1).heightIs(40).leftSpaceToView(_deviceUserTitle, -1)
    .topEqualToView(_deviceUserTitle);
    
    
    
//    [self setupAutoHeightWithBottomView:_deviceUserTitle bottomMargin:12];
    
    
}



-(void)setDataModel:(ReportProjectDeviceInfoModel *)data typeName:(nonnull NSString *)typeName riskName:(nonnull NSString *)riskName{
    if(data != nil){
        _alarmType.text  = typeName;
        _risk.text = riskName;
        _deviceName.text= data.efairydevice_name;
        _deviceType.text = data.efairydevice_device_type;
        _alarm.text = data.efairydevice_alarm_times?data.efairydevice_alarm_times:@"0";
        _warning.text = data.efairydevice_early_warning_times?data.efairydevice_early_warning_times:@"0";
        _trouble.text = data.efairydevice_trouble_times ? data.efairydevice_trouble_times:@"0";
        _deviceAddress.text =(data.efairydevice_address && data.efairydevice_address.length > 0)?data.efairydevice_address:@"暂无";
        
        if(data.efairydevice_user_info_list != nil  && data.efairydevice_user_info_list.count > 0){
            for(int i = 0; i < data.efairydevice_user_info_list.count; i++){
                DeviceUserInfo *info = data.efairydevice_user_info_list[i];
                [self addUserLabel:info index:i maxIndex:(int)data.efairydevice_user_info_list.count-1];
            }
//            NSMutableArray<DeviceUserInfo *>
        }else{
            [self addUserLabel:nil index:0 maxIndex:0];
        }
    }
}


-(void)addUserLabel:(DeviceUserInfo *)info index:(int)index maxIndex:(int)maxIndex{
    UILabel *userTitle = [[UILabel alloc]init];
    
    userTitle.text = info?info.efairyuser_nickname:@"未分配设备拥有者";
    userTitle.textColor = ColorFromRGB(0x666666);
    userTitle.font = [UIFont systemFontOfSize:14.f];
    userTitle.layer.borderColor = [ColorFromRGB(0xB3B8BE)CGColor];
    userTitle.layer.borderWidth = 1.0f;
    userTitle.textAlignment = NSTextAlignmentCenter;
    
    UILabel *userPhone =[[UILabel alloc]init];
    userPhone = [[UILabel alloc]init];;
    userPhone.text = info?info.efairyuser_phonenumber:@"未分配设备拥有者电话";
    userPhone.textColor = ColorFromRGB(0x666666);
    userPhone.font = [UIFont systemFontOfSize:14.f];
    userPhone.layer.borderColor = [ColorFromRGB(0xB3B8BE)CGColor];
    userPhone.layer.borderWidth = 1.0f;
    userPhone.textAlignment = NSTextAlignmentCenter;
    [self.contentView sd_addSubviews:@[userTitle,userPhone]];
    
    userTitle.sd_layout.widthIs((SCREEN_WIDTH-24)/2).heightIs(40).leftSpaceToView(self.contentView, 12)
    .topSpaceToView(_deviceUserTitle, (index*39)-1);
    
    userPhone.sd_layout.widthIs((SCREEN_WIDTH-24)/2+1).heightIs(40).leftSpaceToView(userTitle, -1)
    .topSpaceToView(_deviceUserTitle,(index*39)-1);
    
 
    if(info || index == maxIndex){
        [self.contentView sd_addSubviews:@[_line2,_alarmTypeUIView,_alarmDates,_score,_riskUIView]];
        
        _line2.sd_layout.widthIs(SCREEN_WIDTH).heightIs(8).topSpaceToView(_deviceUserTitle, (index+1)*39-1 +12);
        _alarmTypeUIView.sd_layout.widthIs(44).heightIs(50).leftSpaceToView(self.contentView, -1)
        .topSpaceToView(_line2, 0);
        
        _alarmDates.sd_layout.widthIs((SCREEN_WIDTH-88)/2+10+4).heightIs(50).leftSpaceToView(_alarmTypeUIView, -1)
        .topSpaceToView(_line2,0);
        
        _score.sd_layout.widthIs((SCREEN_WIDTH-88)/2-10).heightIs(50).leftSpaceToView(_alarmDates, -1)
        .topSpaceToView(_line2,0);
    
        _riskUIView.sd_layout.widthIs(44).heightIs(50).leftSpaceToView(_score, -1)
        .topSpaceToView(_line2,0);
        [self setupAutoHeightWithBottomView:_alarmDates bottomMargin:0];
//        [self setupAutoHeightWithBottomViewsArray:@[_alarmDates] bottomMargin:0];
    }
}


-(void)onclickAlarmTypeView:(id)tap{
    __weak typeof(self) weakSelf = self;
    if(weakSelf.chooseAlarmTypeBlock){
        weakSelf.chooseAlarmTypeBlock();
    }
}


-(void)onclickRiskView:(id)tap{
     __weak typeof(self) weakSelf = self;
    if(weakSelf.chooseRiskTypeBlock){
        weakSelf.chooseRiskTypeBlock();
    }
}
@end
